package net.caixiaomi.info.ui.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.interfaces.MatchingNumsCallBack;
import net.caixiaomi.info.interfaces.MatchingSelectCallBack;

import butterknife.BindView;

/**
 * Created by suntian on 2018/7/20.
 */

public class MatchingSelectView extends LinearLayout implements View.OnClickListener {

    private LinearLayout llt_title;
    private RelativeLayout match_before_group,
            match_after_group,
            match_mine_group;

    private View cursor;

    private int currentIndex;//当前页卡编号
    public int subTitleWidth;//子标题宽度，cursor平移距离
    private int mLineWidth; //每个条目的宽度
    private int mSpaceWidth;    //每个条目文字的间隙

    private int mScreenWidth;
    private TextView mBeforeTv;
//    private TextView mBeforeNumTv;
    private TextView mMineTv;
//    private TextView mMineNumTv;
    private TextView mAfterTv;
//    private TextView mAfterNumTv;

    private MatchingSelectCallBack mCallBack;

    public MatchingSelectView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mScreenWidth=CommonApp.mScreenWidth;
        View view=View.inflate(context, R.layout.matching_select_bar_layout,null);
        addView(view);

        llt_title = (LinearLayout) findViewById(R.id.llt_title);
        mBeforeTv= (TextView) findViewById(R.id.match_before_tv);
//        mBeforeNumTv= (TextView) findViewById(R.id.match_before_num_tv);
        mMineTv= (TextView) findViewById(R.id.match_mine_tv);
//        mMineNumTv= (TextView) findViewById(R.id.match_mine_num_tv);
        mAfterTv= (TextView) findViewById(R.id.match_after_tv);
//        mAfterNumTv= (TextView) findViewById(R.id.match_after_num_tv);

        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) llt_title.getLayoutParams();
        params.width = mScreenWidth;
        llt_title.setLayoutParams(params);

        match_before_group = (RelativeLayout) findViewById(R.id.match_before_group);
        match_after_group = (RelativeLayout) findViewById(R.id.match_after_group);
        match_mine_group = (RelativeLayout) findViewById(R.id.match_mine_group);
        cursor = findViewById(R.id.cursor);

        match_before_group.setOnClickListener(this);
        match_after_group.setOnClickListener(this);
        match_mine_group.setOnClickListener(this);

        subTitleWidth=mScreenWidth/3;

        post(new Runnable() {
            @Override
            public void run() {
                initTabLine();
                setPageSelected(0);
            }
        });
    }

    public void setCallBack(MatchingSelectCallBack callBack){
        mCallBack=callBack;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.match_before_group:
                mCallBack.matchingBarSelected(0);
                break;
            case R.id.match_after_group:
                mCallBack.matchingBarSelected(1);
                break;
            case R.id.match_mine_group:
                mCallBack.matchingBarSelected(2);
                break;
        }
    }

    public void setPageSelected(int position){
        Animation animation = new TranslateAnimation(
                currentIndex * subTitleWidth+mSpaceWidth,  //from X
                position * subTitleWidth+mSpaceWidth,      //to X
                0, 0
        );

        //cursor滑动
        currentIndex = position;
        animation.setFillAfter(true);//动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setInterpolator(new DecelerateInterpolator());//减速动画
        animation.setDuration(300);//动画持续时间0.3秒
        cursor.startAnimation(animation);
        llt_title.getChildAt(position).requestFocus();

        //title的text变色
        setMatchingSelect(position);
    }

    /**
     * 初始化tab下划线
     * 实现步骤：
     * 1、获取屏幕大小 比例设置宽度
     * 2、然后有几个tab就分几份  移动时改变外边距参数
     */
    private void initTabLine() {
        mLineWidth = mScreenWidth / 3;
        mSpaceWidth=(mLineWidth-mBeforeTv.getMeasuredWidth())/2;

        RelativeLayout.LayoutParams mLineparams = (RelativeLayout.LayoutParams) cursor.getLayoutParams();
        mLineparams.width = mBeforeTv.getMeasuredWidth();
        cursor.setLayoutParams(mLineparams);
    }


    /**
     * 选择比赛栏目
     */
    private void setMatchingSelect(int type){

        clearMatchingSelect();

        switch (type){
            case 0: //未结束
                mBeforeTv.setTextColor(getResources().getColor(R.color.orange_primary));
//                mBeforeNumTv.setBackgroundResource(R.drawable.match_title_ses_set);

                break;
            case 1: //已结束
                mAfterTv.setTextColor(getResources().getColor(R.color.orange_primary));
//                mAfterNumTv.setBackgroundResource(R.drawable.match_title_ses_set);

                break;
            case 2: //我的比赛
                mMineTv.setTextColor(getResources().getColor(R.color.orange_primary));
//                mMineNumTv.setBackgroundResource(R.drawable.match_title_ses_set);

                break;
        }
    }

    private void clearMatchingSelect(){
        mBeforeTv.setTextColor(getResources().getColor(R.color.third_text));
        mMineTv.setTextColor(getResources().getColor(R.color.third_text));
        mAfterTv.setTextColor(getResources().getColor(R.color.third_text));

//        mBeforeNumTv.setBackgroundResource(R.drawable.match_title_ses_nor);
//        mMineNumTv.setBackgroundResource(R.drawable.match_title_ses_nor);
//        mAfterNumTv.setBackgroundResource(R.drawable.match_title_ses_nor);
    }

}
